package com.agu.mgt.fsmsmgt.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.agu.entity.fsmsmgt.FsmsTemplateCanvasEntity;
import com.agu.entity.fsmsmgt.FsmsTemplateEntity;
import com.agu.mgt.fsmsmgt.dao.FsmsTemplateCanvasDao;
import com.agu.mgt.fsmsmgt.dao.FsmsTemplateDao;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service("fsmsTemplateService")
@Transactional(readOnly = true)
public class FsmsTemplateService{
	@Autowired
	private FsmsTemplateDao fsmsTemplateDao;
	@Autowired
	private FsmsTemplateCanvasDao fsmsTemplateCanvasDao;
	
	public FsmsTemplateEntity queryObject(Integer templateId){
		
		FsmsTemplateEntity template =	fsmsTemplateDao.queryObject(templateId);
		List<FsmsTemplateCanvasEntity> list = fsmsTemplateDao.queryCanvasList(templateId);
		template.setCavnenList(list);
		
		return template;
	}
	
	
	public List<FsmsTemplateEntity> queryList(Map<String, Object> map){
		return fsmsTemplateDao.queryList(map);
	}

    
	public Page<FsmsTemplateEntity> queryPageList(Query query){
	    PageHelper.startPage(query.getPage(), query.getLimit());
	    return fsmsTemplateDao.queryPageList(query);
	}
	
	
	public int queryTotal(Map<String, Object> map){
		return fsmsTemplateDao.queryTotal(map);
	}
	
	
	@Transactional
	public void save(FsmsTemplateEntity fsmsTemplate){
		
		fsmsTemplateDao.save(fsmsTemplate);
		for(FsmsTemplateCanvasEntity tc:   fsmsTemplate.getCavnenList()) {
			tc.setTemplateId(fsmsTemplate.getTemplateId());
			tc.setCreateTime(new Date());
			fsmsTemplateCanvasDao.save(tc);
		}

	}
	
	
	@Transactional
	public void update(FsmsTemplateEntity fsmsTemplate,Set<Integer> idList){
		
		Set<Integer> newidList = new HashSet();
		
		for(FsmsTemplateCanvasEntity cavas: fsmsTemplate.getCavnenList())
		{
			newidList.add(cavas.getCanvasId());
		}
		
		Set<Integer> idAddList = new HashSet();
		Set<Integer> idDelList = new HashSet();
		Set<Integer> idModifyList = new HashSet();
		
		idDelList.addAll(idList);
		idAddList.addAll(newidList);
		idModifyList.addAll(newidList);
		
		idDelList.removeAll(newidList);
		idAddList.removeAll(idList);
		
		idModifyList.removeAll(idDelList);
		idModifyList.removeAll(idAddList);
		
		
		fsmsTemplateDao.update(fsmsTemplate);
		
		for(Integer canvasId:idDelList)
		{
			fsmsTemplateCanvasDao.delete(canvasId);
		}
		
		for(FsmsTemplateCanvasEntity tc:   fsmsTemplate.getCavnenList()) {
						
			if(idAddList.contains(tc.getCanvasId()))
			{
				tc.setTemplateId(fsmsTemplate.getTemplateId());
				tc.setCreateTime(new Date());
				fsmsTemplateCanvasDao.save(tc);
			}
			
			if(idModifyList.contains(tc.getCanvasId()))
			{
				fsmsTemplateCanvasDao.update(tc);
			}
		
		}

	}
	
	
	@Transactional
	public void delete(Integer templateId){
		fsmsTemplateDao.delete(templateId);
	}
	
	
	@Transactional
	public void deleteBatch(Integer[] templateIds){
		fsmsTemplateDao.deleteBatch(templateIds);
	}
}
